<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
    <script>
    function reload(form){
    var val=form.kamer.options[form.kamer.options.selectedIndex].value;
    self.location='onderdeelprobleem.php?kamer=' + val ;
    }
    function reload2(form){
    var val=form.kamer.options[form.kamer.options.selectedIndex].value;
    var val2=form.onderdeel.options[form.onderdeel.options.selectedIndex].value;
    self.location='onderdeelprobleem.php?kamer=' + val + '&onderdeel=' + val2 ;
    }
    </script>
<hgroup>
<h1>Onderdeel-problemen</h1>
</hgroup>
    <input type="button" value="Terug naar kamer-onderdelen" onclick="javascript:history.back();" /><br><br>
    <form name="kamerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <select onchange="reload(this.form)" name="kamer">
            <option value="0">Kies een kamer</option>
            <?php
            $sql = "SELECT * from roomtype";
            $result = mysql_query($sql) or die (mysql_error());
            while($rij = mysql_fetch_array($result))
            {
                if (isset($_GET['kamer']))
                {
                    if ($_GET['kamer'] == $rij['Id'])
                    {
                        echo "<option selected value=\"".$rij['Id']."\">".$rij['Name']."</option>";
                    }
                    else
                    {
                        echo "<option value=\"".$rij['Id']."\">".$rij['Name']."</option>";
                    }
                }
                else
                {
                    echo "<option value=\"".$rij['Id']."\">".$rij['Name']."</option>";
                }
            }
            ?>
    </select>
    <select onchange="reload2(this.form)" name="onderdeel">
            <option value="0"><?php if (isset($_GET['kamer'])) { echo "Kies een onderdeel"; } else { echo "Kies eerst een kamer"; } ?></option>
            <?php
            if (isset($_GET['kamer']))
            {
                $sql2 = "SELECT * from roomitem where RoomId=".$_GET['kamer']."";
                $result2 = mysql_query($sql2) or die (mysql_error());
                while($row = mysql_fetch_array($result2))
                {
                    if (isset($_GET['onderdeel']))
                    {
                        if ($_GET['onderdeel'] == $row['Id'])
                        {
                            echo "<option selected value=\"".$row['Id']."\">".$row['Name']."</option>";
                        }
                        else
                        {
                            echo "<option value=\"".$row['Id']."\">".$row['Name']."</option>";
                        }
                    }
                    else
                    {
                        echo "<option value=\"".$row['Id']."\">".$row['Name']."</option>";
                    }
                }
            }
            ?>
    </select>
    </form>
    <br>
    
<?php
if (isset($_GET['kamer']) && isset($_GET['onderdeel']))
{
    echo "Klik <a href=\"onderdeelprobleemtoevoegen.php?id=".$_GET['onderdeel']."&kamer=".$_GET['kamer']."\" >hier</a> om een hoofd-probleem toe te voegen.";
    $sql2 = "SELECT * FROM itemproblem where ItemId=".$_GET['onderdeel']."";
    $result2 = mysql_query($sql2) or die (mysql_error());
    
    #for avoiding some errors
    if(mysql_num_rows($result2) > 0)
    {
    #start the list
    echo "<ul>";
    
    while($row = mysql_fetch_array($result2))
    {
        if ($row['ParentId'] == -1)
        {
            #print the item, you can also make links out of these
            echo "<li>".$row['Name']." <a href=\"onderdeelkindprobleemtoevoegen.php?id=".$_GET['onderdeel']."&parent=".$row['Id']."&kamer=".$_GET['kamer']."\"><img src=\"../../images/add.png\" alt=\"Kind-probleem toevoegen\"></a>";
            echo " <a href=\"onderdeelprobleemaanpassen.php?id=".$row['Id']."&kamer=".$_GET['kamer']."&onderdeel=".$_GET['onderdeel']."\"><img src=\"../../images/edit.png\" alt=\"Probleem aanpassen\"></a></li>";
            #recursive function(made in next step) for getting all the subs by passing id of main item
            get_children($row['Id']);
        }
    }
    #end the list
    echo "</ul>";
    #some message if the database is empty
    }
    else
    {
        echo '<br><br>Er zijn nog geen problemen toegevoegd voor dit onderdeel';
    }

    #clear the memory
    mysql_free_result($result2);
}

    function get_children($parent, $level = 1)
    {
   
    $sql3 = "SELECT * FROM itemproblem WHERE ParentId = ".$parent."";
    $result3 = mysql_query($sql3) or die (mysql_error());
    
    
    #for avoiding some errors
    if(mysql_num_rows($result3) > 0)
    {
        #start the list
        echo '<ul>';

        while($row = mysql_fetch_array($result3))
        {
            #print the item, you can also make links out of these
            echo "<li>".$row['Name']." <a href=\"onderdeelkindprobleemtoevoegen.php?id=".$_GET['onderdeel']."&parent=".$row['Id']."&kamer=".$_GET['kamer']."\"><img src=\"../../images/add.png\" alt=\"Kind-probleem toevoegen\"></a>";
            echo " <a href=\"onderdeelprobleemaanpassen.php?id=".$row['Id']."&kamer=".$_GET['kamer']."&onderdeel=".$_GET['onderdeel']."\"><img src=\"../../images/edit.png\" alt=\"Probleem aanpassen\"></a></li>";
            #recursive function(made in next step) for getting all the subs by passing id of main item
            get_children($row['Id'], $level+1);
        }

        #close the list
        echo '</ul>';
        }
    }

?>
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>